package com.happyfamily.springboot.crowdsourcingplatform.controller.sun;

import com.google.gson.Gson;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.TableModel;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.SysUserService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.WorkService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/api-front/work/")
public class WorkController
{
	@Autowired
	private WorkService workServiceImpl;

	@Autowired
	private SysUserService userServiceImpl;

	@RequestMapping("getList")
	@ResponseBody
	@ApiOperation(value = "服务商获取自己发布的作品列表", notes = "服务商获取自己发布的作品列表", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商获取自己发布的作品列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public String getList(String limit, String page, HttpServletRequest request)
	{
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		TableModel tableModel = new TableModel();
		tableModel.setMsg("");
		tableModel.setCode(0);
		tableModel.setCount(workServiceImpl.getSum(account));
		tableModel.setData(workServiceImpl.getList(Integer.valueOf(limit), Integer.valueOf(page), account));
		Gson g = new Gson();
		return g.toJson(tableModel);
	}

	@RequestMapping("deleteWork")
	@ResponseBody
	@ApiOperation(value = "服务商对自己发布的作品进行下架", notes = "服务商对自己发布的作品进行下架", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商对自己发布的作品进行下架", level = 4, operationUnit = OperationUnit.USER, operationType = OperationType.DELETE)
	public String deleteWork(String wid, String fname, HttpServletRequest request)
	{
		String str = "no";
		boolean flag = workServiceImpl.deleteWork(Integer.valueOf(wid), fname, request);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}

	@RequestMapping("addWork")
	@ResponseBody
	@ApiOperation(value = "服务商发布作品", notes = "服务商发布作品", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商发布作品", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.INSERT)
	public String addWork(HttpServletRequest request, MultipartFile file) throws Exception
	{
		String str = "no";
		boolean flag = workServiceImpl.addWork(request, file);
		if (flag)
		{
			str = "yes";
		}
		return str;
	}


}